<?php

namespace app\bot\model;

use app\payment\model\Mconfig as MconfigModel;
use app\user\model\User as UserModel;
use think\Model;

/**
 * 配置模型
 */
class BotConfig extends Model
{

    // 设置当前模型对应的完整数据表名称
    protected $name = 'bot_config';
    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    /**
     * 获取配置信息
     * @param  string $name  配置名
     * @param  string $group 配置分组
     * @return mixed
     */
    public static function config($name = '', $group = '')
    {
        $map = [];
        if ($name) {
            $map[] = ['name', '=', $name];
        }
        if ($group) {
            $map[] = ['group', '=', $group];
        }

        $configs = self::where($map)->column('value,type', 'name');

        $result = [];
        foreach ($configs as $config) {
            switch ($config['type']) {
                case 'array':
                    $result[$config['name']] = parse_attr($config['value']);
                    break;
                case 'checkbox':
                    if ($config['value'] != '') {
                        $result[$config['name']] = explode(',', $config['value']);
                    } else {
                        $result[$config['name']] = [];
                    }
                    break;
                default:
                    $result[$config['name']] = $config['value'];
                    break;
            }
        }

        return $name != '' ? $result[$name] : $result;
    }

    /**
     * 获取所有已经支付配置完成的商户号和名称
     * @param  string $merchant_id 商户号
     * @return array
     */
    public static function allMerchantConfig($merchant_id = '')
    {
        $map = $merchant_id ? ['id' => $merchant_id] : [];
        $Mconfigs = MconfigModel::where($map)->column('id');
        $Users = UserModel::column('nickname', 'merchant_id');

        return getOneTwoArray($Mconfigs, $Users);
    }
}
